package cn.edu.dutir.test.unit.utility.encoding;

import java.util.Date;

import cn.edu.dutir.utility.encoding.Encoding;
import cn.edu.dutir.utility.encoding.zhcode;

public class EncodingTransformer {
	private static zhcode zhcoder = new zhcode();

	private static String srcFileOrDir = "E:/Corpus/NTCIR-6/BIG5/CIRB040";
	private static String dstFileOrDir = "E:/Corpus/NTCIR-6/GBK/CIRB040";
	/**
	 * Partial encoding code: GB2312 = 0 GBK = 1 HZ = 3 BIG5 = 4 UTF8 = 6
	 * UNICODES = 11
	 */
	private static int srcEncoding = 1; // HZ
	private static int dstEncoding = 6; // GB2312

	public static void dohelp() {
		System.err
				.println("Usage:\t CodingTransformer -[abcgkhinrstu3678]"
						+ "[abcgkhinrstu3678] <input_file_or_dir> <output_file_or_dir>");
		System.out.println("\ta -- ASCII");
		System.out.println("\tb -- BIG5");
		System.out.println("\tc -- CNS11643");
		System.out.println("\tg -- GB2312");
		System.out.println("\tk -- GBK");
		System.out.println("\th -- HZ");
		System.out.println("\ti -- ISO-2022-CN");
		System.out.println("\tn -- ISO-2022-CN-CNS");
		System.out.println("\tr -- EUC_KR");
		System.out.println("\ts -- Unicode (simplified characters)");
		System.out.println("\tt -- Unicode (traditional characters)");
		System.out.println("\tu -- UNICODE");
		System.out.println("\t3 -- GB18030");
		System.out.println("\t6 -- UTF8S");
		System.out.println("\t7 -- UTF8T");
		System.out.println("\t8 -- UTF8");
	}

	private static void parseOption(String option) {
		if (option.length() != 3 || !option.startsWith("-")) {
			dohelp();
			System.exit(1);
		}
		int codeTypes[] = new int[2];
		option = option.toLowerCase();
		char codeType;
		try {

			for (int i = 0; i < 2; i++) {
				codeType = option.charAt(i + 1);
				if (codeType == 'g') {
					codeTypes[i] = Encoding.GB2312;
				} else if (codeType == 'h') {
					codeTypes[i] = Encoding.HZ;
				} else if (codeType == 'b') {
					codeTypes[i] = Encoding.BIG5;
				} else if (codeType == 'c') {
					codeTypes[i] = Encoding.CNS11643;
				} else if (codeType == '8') {
					codeTypes[i] = Encoding.UTF8;
				} else if (codeType == '6') {
					codeTypes[i] = Encoding.UTF8S;
				} else if (codeType == '7') {
					codeTypes[i] = Encoding.UTF8T;
				} else if (codeType == 'u') {
					codeTypes[i] = Encoding.UNICODE;
				} else if (codeType == 't') {
					codeTypes[i] = Encoding.UNICODET;
				} else if (codeType == 's') {
					codeTypes[i] = Encoding.UNICODES;
				} else if (codeType == 'i') {
					codeTypes[i] = Encoding.ISO2022CN;
				} else if (codeType == '2') {
					codeTypes[i] = Encoding.ISO2022CN_GB;
				} else if (codeType == 'n') {
					codeTypes[i] = Encoding.ISO2022CN_CNS;
				} else if (codeType == '3') {
					codeTypes[i] = Encoding.GB18030;
				} else if (codeType == 'k') {
					codeTypes[i] = Encoding.GBK;
				} else if (codeType == 'r') {
					codeTypes[i] = Encoding.EUC_KR;
				} else if (codeType == 'a') {
					codeTypes[i] = Encoding.ASCII;
					zhcoder.autodetect = true;
				}
			}
			srcEncoding = codeTypes[0];
			dstEncoding = codeTypes[1];
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void parseArgs(String args[]) {
		parseOption(args[0]);
		srcFileOrDir = args[1];
		dstFileOrDir = args[2];
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 3) {
			dohelp();
			System.exit(1);
		}
		Date start = new Date();
		parseArgs(args);

		zhcoder.convertFile(srcFileOrDir, dstFileOrDir, srcEncoding,
				dstEncoding);

		Date end = new Date();
		System.out.println("Time: " + (end.getTime() - start.getTime()) / 1000
				+ " s");
	}
}
